 LST OFF,NOASYM,NOVSYM,NOGEN
 X65816  ; Must go here for 24 bit expressions.
 XREFSLOT 6
 SEG $00
***********************************************************
*
*       ProDOS 8 CORTLAND CLOCK DRIVER
*
*         COPYRIGHT APPLE COMPUTER, INC., 1986
*
*         ALL RIGHTS RESERVED
*
*       Written by Kerry Laidlaw, 2/12/86
*       Modified by Mike Askins, 9/6/86   
*       Modified by Fern Bachman, 9/7/86   
*
***********************************************************
*
* This is the ProDOS8 Cortland built-in clock driver. 
* Its sole function in life is to fetch the time from the Cortland
* clock via the Read Hex Time misc. tool call, and transfer this
* time into the ProDOS global page time format.
*
* This routine will IGNORE any errors passed back to it from the
* Read Hex Time call.  This was done since existing ProDOS8 programs
* cannot deal with some new time error code.
* Thus the only way that a user can tell if his Cortland clock is
* broken, is by noticing that the date and time fields are zeroed.
*
* Note: There are some interesting facts to know regarding the 
* slot clock driver for ProDOS8 and the built-in
* Cortland clock.  The year value returned from the Cortland clock
* is an offset from the year 1900.  Thus Cortland is capable of 
* reporting the year correctly until 1900+255=2155.  Only 7 bits
* are used for the year in the ProDOS8 global page, so theoretically
* 1900+127=2027 is the last year that ProDOS could represent on a
* Cortland.  But this is only if the ProDOS8 year value is interpreted
* as being an offset from 1900.
*
* Historically, the year value has been interpreted as the binary 
* representation of the last two digits of the year 19xx.
* So this means that programs that display the year as a concatination
* of 19 and the ascii equivalent of the year value will work until 1999.
* And programs that just display the last two digits of the year will
* still work correctly until (20)27 if they convert the year value
* correctly, but ignore any hundredths place digit.
*
* Apple //e's that use slot clocks that utilize the slot clock
* driver have further restrictions of the year value.  The slot
* clock driver calculates the year given the position of the day
* of the week in the month.  This algorithm then uses a year look
* up table that has seven possible values.  Leap years are repeated
* in the table.  Since 1988 is a leap year, then the updated slot
* clock driver (file TCLOCK) will yield the six year offset values
* rather then seven.
* So before 1992, if ProDOS8 still exists, the slot clock driver
* routine must be updated again!
*
* So, we now have the following definition:
*    The value placed in the year field is defined as the
*    number of years past the year 1900.
*    Numerically speaking: Current Year = 1900 + year value.
*
*
statereg EQU $C068 ;Cortland memory state register
clock.begin equ $d742 ; Entry address of Cortland clock driver.
MISC.TSN equ 3 ; Misc. tool set number.
Read.Time.Hex equ $0d ; Read Time Hex function number.
dispatch equ $e10000 ; Tool Dispatcher entry address.
minutes equ $bf92 ; Minutes in ProDOS global page.
hours equ $bf93 ;
year equ $bf91 ;
day equ $bf90 ;
*
 org clock.begin
*
************************* See Note #66,67 *************************
*
* This mod will force read/write main memory for the tool
* call by resetting the read/write auxillary memory bits
* in the state register (statereg).
*
 memory8
 sep #$30 ;Make sure we're in 8 bit mode
 lda statereg ;Get the state reg
 sta savestate ;Keep for restore after tool call
 and #$CF ;Clear the Read/Write aux memory bits
 sta statereg ;Make it real
*
***************************************************************
*
 INDEX16
 MEMORY16
*
* First off, lets get into native mode with 16 bit m & x.
*
 clc ; Set e = 0, to set native mode.
 xce ;
 rep #$30 ; Zero m & x for 16 bit mode.
 lda #0 ; Zero out result space.
 pha ; Push 4 words for hex time result...
 pha ;
 pha ;
 pha ;
 ldx #Read.Time.Hex*$100+MISC.TSN ; Read Time Hex.
 jsl =dispatch ; Make the ReadTimeHex call...
*
* Note that no error condition is checked for, so the date will
* be zeroed by default if an error indeed happened.
*
*  Back to 8 bit m to access results on stack...
*
 MEMORY8
 sep #$20 ; 8 bit m
*
************************* See Note #66 *************************
*
 lda savestate ;Restore the state register
 sta statereg
*
***************************************************************
*
*
* Now let's pull the time off the stack and stick it in the global page.
*
 pla ; Pull off Seconds, and ignore.
 pla ; Pull off Minutes.
 sta minutes ; Store in global page.
 pla ; Pull off Hours.
 sta Hours ; Store in global page.
 pla ; Pull off Year value.
 sta year ; Store in global page.
 pla ; Pull off Day.
 inc a ; Increment day value for ProDOS8 format.
 sta day ; Store in global page.
 pla ; Pull off Month.
 inc a ; Inc month value for ProDOS8 format.
 asl a ; Shift month as it sits in between
 asl a ; the year and day values.
 asl a ;
 asl a ;
 asl a ;
 ora day ; Put all but the top bit of month value
 sta day ; in the day byte.
 rol year ; Put hi bit of mo. in lo bit of yr byte.
 pla ; Pull off unused byte.
 pla ; Pull off Day of Week. Stack now clean.
*
 sec ; Now go back to emulation mode
 xce ; to continue with ProDOS8.
 rts ; That's all.
*
savestate dfb 0 ;Keep the state of state register
 asc 'JIMJAYKERRY&MIKE' ;   
clock.end equ *
 ds 125-clock.end+clock.begin,0 ; Zero rest of 125 bytes.
size equ *-clock.begin ; MUST be $7D (125) bytes in length!
